package com.android.bookgarden.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.bookgarden.MainActivity;
import com.android.bookgarden.R;
import com.android.bookgarden.adapter.MsgNotifyAdapter;
import com.android.bookgarden.apis.Apis;
import com.android.bookgarden.base.BaseActivity;
import com.android.bookgarden.bean.DataBean;
import com.android.bookgarden.bean.HomeDataBean;
import com.android.bookgarden.bean.MsgNotifyBean;
import com.android.bookgarden.constract.RealtConstract;
import com.android.bookgarden.network.HttpType;
import com.android.bookgarden.persenter.BasePersenter;
import com.android.bookgarden.recycleview.BaseViewHolder;
import com.android.bookgarden.recycleview.EndLessOnScrollListener;
import com.android.bookgarden.utli.JsonUitl;
import com.android.bookgarden.utli.LogUtil;
import com.android.bookgarden.utli.Utlis;
import com.android.bookgarden.views.LoadingDialog;
import com.lzy.okgo.model.HttpHeaders;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create by zouqh
 * on 2018/11/28
 **/
public class MsgNotifyListActivity extends BaseActivity implements View.OnClickListener ,RealtConstract.View{


    @BindView(R.id.close)
    ImageView close;
    @BindView(R.id.title_text)
    TextView titleText;
    @BindView(R.id.ritgh_icon)
    ImageView ritghIcon;
    @BindView(R.id.ritgh_text)
    TextView ritghText;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    private MsgNotifyAdapter adapter;
    private List<MsgNotifyBean> list;
    private int pageIndex = 1;
    private int pageSize = 20;
    private RealtConstract.Presenter presenter;
    private String deleteId;
    private int poss;
    private boolean flag = false;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_msg_notify;
    }

    @Override
    protected void initView() {
        close.setOnClickListener(this);
        list = new ArrayList<>();
        presenter = new BasePersenter(this,this);

        adapter = new MsgNotifyAdapter(list, this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler.setLayoutManager(manager);
        recycler.setHasFixedSize(true);
        recycler.setAdapter(adapter);
        adapter.addFooterView(R.layout.layout_footer);
//        adapter.addHeaderView(R.layout.home_layout_header,advList);

        recycler.addOnScrollListener(new EndLessOnScrollListener(manager) {
            @Override
            public void onLoadMore() {
                if (flag){
                    pageIndex ++;
                    if (list.size() != 0)
                        adapter.setFooterVisible(View.VISIBLE);
                    getDataList();
                }
            }
        });
        adapter.setOnItemClickLitener(new MsgNotifyAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, BaseViewHolder viewHolder) {
                if (!list.get(position).isIsread()){
                    getDelete(position);
                }
            }

            @Override
            public void onLongClick(int postion, BaseViewHolder viewHolder) {

            }
        });
        getDataList();

    }

    @Override
    public void onRequestPermissions(String str, boolean z) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close:
                finish();

                break;
        }

    }

    private void getDataList() {
        LoadingDialog.showMassge(this,"加载中...");
        presenter.loadDataHeader(null, "", Apis.MESSAGE_LIST + pageIndex + "/" + pageSize , 1002, Utlis.getHeadler(this), HttpType.GET);
    }

    private void getDelete(int id) {
        poss = id;
        LoadingDialog.showMassge(this,"删除中...");
        deleteId = ""+list.get(id).getId();
        presenter.loadDataHeader(null, "", Apis.MESSAGE_DELETE +deleteId , 1003, Utlis.getHeadler(this), HttpType.DELETE);
    }


    @Override
    public void returnData(String data, int urlCode) {
        LogUtil.e("返回数据", data + "");

        LoadingDialog.hideProgress();
        handler.obtainMessage(urlCode, data).sendToTarget();
    }

    @Override
    public void LoadError(String errorMsg, int urlCode) {
        showToast("请求失败");
        LoadingDialog.hideProgress();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String data = (String) msg.obj;
            switch (msg.what) {
                case 1001:
                    try {
                        LoadingDialog.hideProgress();

                        DataBean bean = Utlis.getDataBean(data);
                        showToast(bean.getMessage());
                        if (bean.getCode() == 200) {
                            finish();
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;

                case 1002:
//                    refreshLayout.setRefreshing(false);
                    if (TextUtils.isEmpty(data)){
                        adapter.setFooterVisible(View.GONE);
                        flag = false;
                    }
                    DataBean bean = Utlis.getDataBean(data);
                    if (null == bean) {
                        showToast("获取列表失败");
                        return;
                    }
                    if (bean.getCode() == 200) {
                        List<MsgNotifyBean> listD = JsonUitl.stringToList(bean.getData(), MsgNotifyBean.class);
                        if (null == listD || listD.size() <= 0) {
                            adapter.setFooterVisible(View.GONE);
                            flag = false;
                            return;
                        }
                        list.addAll(listD);
                        adapter.notifyDataSetChanged();
                        if (listD.size()<pageSize){
                            adapter.setFooterVisible(View.GONE);
                            flag = false;
                        }else {
                            flag = true;
                        }

                    } else {
                        showToast(bean.getMessage() + "");
                    }


                    break;
                case 1003:
                    try {
                        LoadingDialog.hideProgress();
                        bean = Utlis.getDataBean(data);
                        showToast(bean.getMessage());
                        if (bean.getCode() == 200) {
//                          list.remove(poss);
//                          adapter.notifyDataSetChanged();
                            list.get(poss).setIsread(true);
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;

            }
        }
    };

}
